package com.mallsystem.adminservice.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.mallsystem.common.bean.Admin;
import com.mallsystem.common.bean.RespBean;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.AdminService;
import com.mallsystem.common.utils.JwtUtil;
import com.mallsystem.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AdminService adminService;

    @GetMapping
    public RespBean selectAll(){
        return RespBean.ok("",adminService.selectAll());
    }
    @GetMapping("/captcha")
    public RespBean captcha(){
        //创建验证码对象
        LineCaptcha captcha = new LineCaptcha(120, 38, 4, 10);
        //生成唯一的key - 雪花算法
        String key = IdUtil.getSnowflakeNextIdStr();
        //将key 和 验证码文本保存在Redis中
        redisUtil.set(key,captcha.getCode(),60);
        //将验证码图片转换成Base64编码形式
        String imageBase64Data = captcha.getImageBase64Data();
        //将key和Base64编码保存在map中
        Map<String,Object> map = new HashMap<>();
        map.put("key",key);
        map.put("imageBase64Data",imageBase64Data);
        //将key和Base64图片返回
        return RespBean.ok("成功生成验证码",map);
    }
    @PostMapping("/login")
    public RespBean login(String username, String password,String captchaInput,String key) throws AllException {
        //判断验证码是否正确 - 从Redis中查询验证码进行判断
        String captcha = (String) redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new AllException("验证码错误，请重新登录");
        }
        //登录
        Admin admin = adminService.login(username, password);
        //生成token
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",admin.getId());
        map.put("username",admin.getUsername());
        map.put("role","admin");

        String jwtStr = JwtUtil.generateJwt(map);
        //token返回给前端
        return RespBean.ok("登陆成功",jwtStr);
    }
    @GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id查询用户信息
        Integer id = (Integer) map.get("id");
        Admin admin = adminService.selectById(id);
        //返回
        return RespBean.ok("",admin);
    }
    //修改密码
    @PutMapping("/chgPwd")
    public RespBean chgPwd(String oldPwd,String newPwd,String newPwd1,@RequestHeader("token") String token) throws AllException {
        if(!newPwd.equals(newPwd1)){
            throw new AllException("两次输入密码前后不一致，请重新输入");
        }
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id查询用户信息
        Integer id = (Integer) map.get("id");
        adminService.chgPwd(oldPwd,newPwd,id);
        return RespBean.ok("修改密码成功，请重新登陆");
    }
    //修改管理员信息
    @PutMapping("/chgInfo")
    public RespBean chgInfo(@RequestBody Admin admin){
        admin.setUsername(null);
        adminService.update(admin);
        return RespBean.ok("修改个人信息成功！");
    }
    //添加管理员
    @PostMapping
    public RespBean insert(@RequestBody Admin admin) throws AllException {
        adminService.insert(admin);
        return RespBean.ok("添加成功");
    }
    //重置密码
    @PutMapping("/password")
    public RespBean updatePassword(@RequestBody Admin admin) throws AllException {
        adminService.updatePassword(admin);
        return RespBean.ok("重置密码成功");
    }
    //修改状态
    @PutMapping("/status")
    public RespBean updateStatus(@RequestBody Admin admin) throws AllException {
        adminService.updateStatus(admin);
        return RespBean.ok("状态修改成功");
    }
    //查询前5的分类
    @GetMapping("/top5")
    public RespBean selectCategoryTop5(){
        return RespBean.ok("",adminService.selectCategoryTop5());
    }
    //每日订单数量
    @GetMapping("/orderToday")
    public RespBean selectOrderByDay(){
        return RespBean.ok("",adminService.selectOrderByDay());
    }
}
